package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.FixedAssetTransfer;
import com.amsp.oa.oademo.mapper.FixedAssetTransferMapper;
import com.amsp.oa.oademo.service.FixedAssetTransferService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

@Service
public class FixedAssetTransferServiceImpl extends ServiceImpl<FixedAssetTransferMapper, FixedAssetTransfer>
        implements FixedAssetTransferService {

    @Override
    public Page<FixedAssetTransfer> getPage(Integer current, Integer size, String transferNo, String assetName,
            String assetNumber, String outDepartment, String inDepartment, String startDate, String endDate) {
        Page<FixedAssetTransfer> page = new Page<>(current, size);

        LambdaQueryWrapper<FixedAssetTransfer> queryWrapper = new LambdaQueryWrapper<>();

        // 添加查询条件
        queryWrapper.like(transferNo != null && !transferNo.isEmpty(),
                FixedAssetTransfer::getTransferNo, transferNo)
                .like(assetName != null && !assetName.isEmpty(),
                        FixedAssetTransfer::getAssetName, assetName)
                .like(assetNumber != null && !assetNumber.isEmpty(),
                        FixedAssetTransfer::getAssetNumber, assetNumber)
                .like(outDepartment != null && !outDepartment.isEmpty(),
                        FixedAssetTransfer::getOutDepartment, outDepartment)
                .like(inDepartment != null && !inDepartment.isEmpty(),
                        FixedAssetTransfer::getInDepartment, inDepartment);

        // 添加日期范围查询
        if (startDate != null && !startDate.isEmpty()) {
            queryWrapper.ge(FixedAssetTransfer::getCreateTime, startDate + " 00:00:00");
        }
        if (endDate != null && !endDate.isEmpty()) {
            queryWrapper.le(FixedAssetTransfer::getCreateTime, endDate + " 23:59:59");
        }

        // 按创建时间倒序排序
        queryWrapper.orderByDesc(FixedAssetTransfer::getCreateTime);

        return this.page(page, queryWrapper);
    }
}